using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Xml;

namespace CodeEdit
{
	/// <summary>
	/// Summary description for Options.
	/// </summary>
	public class Options : System.Windows.Forms.Form
	{
		private System.Windows.Forms.TabControl tabControl1;
		private System.Windows.Forms.ListBox listBox1;
		private string keywordXmlPath;
		private Hashtable keywordsHash = new Hashtable();
		private System.Windows.Forms.Button openFileBtn;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.TextBox custKeywordPath;
		private System.Windows.Forms.OpenFileDialog openKeywordFile;
		private System.Windows.Forms.Button OKBtn;
		private System.Windows.Forms.TextBox AddKeyword;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Button AddBtn;
		private System.Windows.Forms.Button RemoveBtn;
		private System.Windows.Forms.ColorDialog colorDialog1;
		private System.Windows.Forms.Button ColorBtn;
		private System.Windows.Forms.TextBox KeyColor;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.TabPage KeywordTab;
		private System.Windows.Forms.Button CancelBtn;
		private System.Windows.Forms.TabPage GeneralTab;
		private System.Windows.Forms.GroupBox groupBox1;
		public Hashtable ConfigHash;
		private string ConfigPath;
		private System.Windows.Forms.CheckBox syntaxBox;
		private System.Windows.Forms.CheckBox commentsBox;
		private System.Windows.Forms.TabPage EditorTab;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.Label label5;
		private System.Windows.Forms.ColorDialog BgColor;
		private System.Windows.Forms.ColorDialog FontColor;
		private System.Windows.Forms.Button BgBtn;
		private System.Windows.Forms.Button FontBtn;
		private System.Windows.Forms.TextBox FontLbl;
		private System.Windows.Forms.TextBox BgLbl;
		private System.Windows.Forms.Button ResetBtn;
		private CodeBox code;
		private ColorConverter colConvert;
		private System.Windows.Forms.Button ApplyBtn;
		private System.Windows.Forms.Button CmntBtn;
		private System.Windows.Forms.Label label6;
		private System.Windows.Forms.TextBox CmntColor;
		private System.Windows.Forms.ColorDialog CmntColorDlg;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Options(CodeBox code)
		{
			this.code = code;
			this.ConfigPath = ConfigPath;
			// this.ConfigPath = "C:\\jeff\\hw\\395-0 PED\\meta\\interpreter\\interpreter\\Config.xml";
			
			
			colConvert = new ColorConverter();
			ConfigHash = EditorConfiguration.ReadConfigFile(ConfigPath);
			keywordXmlPath = @ConfigHash["keywords"].ToString();			
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
			custKeywordPath.Text = keywordXmlPath.ToString();
			keywordsHash = EditorConfiguration.ReadXmlToHash(keywordXmlPath);
			FillListBox(keywordsHash);

			GetSyntaxCheckBox((string)ConfigHash["synHigh"]);
			GetCommentsCheckBox((string)ConfigHash["comments"]);
			
			EditorConfiguration info = new EditorConfiguration(ConfigPath);
			BgLbl.Text = info.GetInfo("CurrentBg");
			FontLbl.Text = info.GetInfo("CurrentFont");
			CmntColor.Text = info.GetInfo("CommentColor");
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		// Get the current value for the Syntax Highlighting and
		// sets the syntaxBox accordingly.
		public void GetSyntaxCheckBox(string syntaxHigh)
		{
			if (syntaxHigh == "True")
			{
				syntaxBox.Checked = true;
				EditorConfiguration.doHighlight = true;
			}
			else
			{
				syntaxBox.Checked = false;
				EditorConfiguration.doHighlight = false;
			}
		}

		// Get the current value for the Commenting features and
		// sets the commentsBox accordingly.
		public void GetCommentsCheckBox(string comments)
		{
			if (comments == "True")
			{
				commentsBox.Checked = true;
				EditorConfiguration.doComment = true;
			}
			else
			{
				commentsBox.Checked = false;
				EditorConfiguration.doComment = false;
			}
		}

		// Fills the ListBox with data from the keywordHash
		private void FillListBox(Hashtable hash)
		{
			listBox1.Items.Clear();
			ICollection keys = hash.Keys ;
			ICollection values = hash.Values ;

			foreach (string i in keys)
			{
				listBox1.Items.Add(i);
			}
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Options));
			this.tabControl1 = new System.Windows.Forms.TabControl();
			this.KeywordTab = new System.Windows.Forms.TabPage();
			this.label3 = new System.Windows.Forms.Label();
			this.KeyColor = new System.Windows.Forms.TextBox();
			this.ColorBtn = new System.Windows.Forms.Button();
			this.RemoveBtn = new System.Windows.Forms.Button();
			this.AddBtn = new System.Windows.Forms.Button();
			this.label2 = new System.Windows.Forms.Label();
			this.AddKeyword = new System.Windows.Forms.TextBox();
			this.label1 = new System.Windows.Forms.Label();
			this.openFileBtn = new System.Windows.Forms.Button();
			this.custKeywordPath = new System.Windows.Forms.TextBox();
			this.listBox1 = new System.Windows.Forms.ListBox();
			this.OKBtn = new System.Windows.Forms.Button();
			this.CancelBtn = new System.Windows.Forms.Button();
			this.GeneralTab = new System.Windows.Forms.TabPage();
			this.CmntColor = new System.Windows.Forms.TextBox();
			this.label6 = new System.Windows.Forms.Label();
			this.CmntBtn = new System.Windows.Forms.Button();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.syntaxBox = new System.Windows.Forms.CheckBox();
			this.commentsBox = new System.Windows.Forms.CheckBox();
			this.EditorTab = new System.Windows.Forms.TabPage();
			this.ApplyBtn = new System.Windows.Forms.Button();
			this.ResetBtn = new System.Windows.Forms.Button();
			this.label5 = new System.Windows.Forms.Label();
			this.FontLbl = new System.Windows.Forms.TextBox();
			this.FontBtn = new System.Windows.Forms.Button();
			this.label4 = new System.Windows.Forms.Label();
			this.BgLbl = new System.Windows.Forms.TextBox();
			this.BgBtn = new System.Windows.Forms.Button();
			this.openKeywordFile = new System.Windows.Forms.OpenFileDialog();
			this.colorDialog1 = new System.Windows.Forms.ColorDialog();
			this.BgColor = new System.Windows.Forms.ColorDialog();
			this.FontColor = new System.Windows.Forms.ColorDialog();
			this.CmntColorDlg = new System.Windows.Forms.ColorDialog();
			this.tabControl1.SuspendLayout();
			this.KeywordTab.SuspendLayout();
			this.GeneralTab.SuspendLayout();
			this.groupBox1.SuspendLayout();
			this.EditorTab.SuspendLayout();
			this.SuspendLayout();
			// 
			// tabControl1
			// 
			this.tabControl1.Controls.AddRange(new System.Windows.Forms.Control[] {
																					  this.KeywordTab,
																					  this.GeneralTab,
																					  this.EditorTab});
			this.tabControl1.Name = "tabControl1";
			this.tabControl1.SelectedIndex = 0;
			this.tabControl1.Size = new System.Drawing.Size(472, 416);
			this.tabControl1.TabIndex = 0;
			// 
			// KeywordTab
			// 
			this.KeywordTab.Controls.AddRange(new System.Windows.Forms.Control[] {
																					 this.label3,
																					 this.KeyColor,
																					 this.ColorBtn,
																					 this.RemoveBtn,
																					 this.AddBtn,
																					 this.label2,
																					 this.AddKeyword,
																					 this.label1,
																					 this.openFileBtn,
																					 this.custKeywordPath,
																					 this.listBox1,
																					 this.OKBtn,
																					 this.CancelBtn});
			this.KeywordTab.Location = new System.Drawing.Point(4, 22);
			this.KeywordTab.Name = "KeywordTab";
			this.KeywordTab.Size = new System.Drawing.Size(464, 390);
			this.KeywordTab.TabIndex = 0;
			this.KeywordTab.Text = "Keywords";
			// 
			// label3
			// 
			this.label3.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label3.Location = new System.Drawing.Point(272, 56);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(128, 16);
			this.label3.TabIndex = 12;
			this.label3.Text = "Current Color:";
			// 
			// KeyColor
			// 
			this.KeyColor.Enabled = false;
			this.KeyColor.Location = new System.Drawing.Point(272, 80);
			this.KeyColor.Name = "KeyColor";
			this.KeyColor.Size = new System.Drawing.Size(136, 20);
			this.KeyColor.TabIndex = 11;
			this.KeyColor.Text = "";
			// 
			// ColorBtn
			// 
			this.ColorBtn.Location = new System.Drawing.Point(176, 80);
			this.ColorBtn.Name = "ColorBtn";
			this.ColorBtn.TabIndex = 8;
			this.ColorBtn.Text = "Color ...";
			this.ColorBtn.Click += new System.EventHandler(this.ColorBtn_Click);
			// 
			// RemoveBtn
			// 
			this.RemoveBtn.Location = new System.Drawing.Point(176, 144);
			this.RemoveBtn.Name = "RemoveBtn";
			this.RemoveBtn.TabIndex = 10;
			this.RemoveBtn.Text = "Remove";
			this.RemoveBtn.Click += new System.EventHandler(this.RemoveBtn_Click);
			// 
			// AddBtn
			// 
			this.AddBtn.Location = new System.Drawing.Point(176, 112);
			this.AddBtn.Name = "AddBtn";
			this.AddBtn.TabIndex = 9;
			this.AddBtn.Text = "Add";
			this.AddBtn.Click += new System.EventHandler(this.AddBtn_Click);
			// 
			// label2
			// 
			this.label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label2.Location = new System.Drawing.Point(8, 56);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(128, 16);
			this.label2.TabIndex = 7;
			this.label2.Text = "Add Keyword:";
			// 
			// AddKeyword
			// 
			this.AddKeyword.Location = new System.Drawing.Point(8, 80);
			this.AddKeyword.Name = "AddKeyword";
			this.AddKeyword.Size = new System.Drawing.Size(160, 20);
			this.AddKeyword.TabIndex = 6;
			this.AddKeyword.Text = "";
			// 
			// label1
			// 
			this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label1.Location = new System.Drawing.Point(8, 8);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(200, 16);
			this.label1.TabIndex = 5;
			this.label1.Text = "Select a new  keyword file:";
			// 
			// openFileBtn
			// 
			this.openFileBtn.Image = ((System.Drawing.Bitmap)(resources.GetObject("openFileBtn.Image")));
			this.openFileBtn.Location = new System.Drawing.Point(248, 24);
			this.openFileBtn.Name = "openFileBtn";
			this.openFileBtn.Size = new System.Drawing.Size(32, 23);
			this.openFileBtn.TabIndex = 4;
			this.openFileBtn.Click += new System.EventHandler(this.openFileBtn_Click);
			// 
			// custKeywordPath
			// 
			this.custKeywordPath.Location = new System.Drawing.Point(8, 24);
			this.custKeywordPath.Name = "custKeywordPath";
			this.custKeywordPath.Size = new System.Drawing.Size(232, 20);
			this.custKeywordPath.TabIndex = 3;
			this.custKeywordPath.Text = "";
			// 
			// listBox1
			// 
			this.listBox1.Anchor = ((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
				| System.Windows.Forms.AnchorStyles.Left);
			this.listBox1.Location = new System.Drawing.Point(8, 112);
			this.listBox1.Name = "listBox1";
			this.listBox1.Size = new System.Drawing.Size(160, 238);
			this.listBox1.TabIndex = 2;
			this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);
			// 
			// OKBtn
			// 
			this.OKBtn.Location = new System.Drawing.Point(320, 352);
			this.OKBtn.Name = "OKBtn";
			this.OKBtn.TabIndex = 1;
			this.OKBtn.Text = "OK";
			this.OKBtn.Click += new System.EventHandler(this.OKBtn_Click);
			// 
			// CancelBtn
			// 
			this.CancelBtn.Location = new System.Drawing.Point(232, 352);
			this.CancelBtn.Name = "CancelBtn";
			this.CancelBtn.TabIndex = 0;
			this.CancelBtn.Text = "Cancel";
			this.CancelBtn.Click += new System.EventHandler(this.CancelBtn_Click);
			// 
			// GeneralTab
			// 
			this.GeneralTab.Controls.AddRange(new System.Windows.Forms.Control[] {
																					 this.CmntColor,
																					 this.label6,
																					 this.CmntBtn,
																					 this.groupBox1});
			this.GeneralTab.Location = new System.Drawing.Point(4, 22);
			this.GeneralTab.Name = "GeneralTab";
			this.GeneralTab.Size = new System.Drawing.Size(464, 390);
			this.GeneralTab.TabIndex = 1;
			this.GeneralTab.Text = "General";
			// 
			// CmntColor
			// 
			this.CmntColor.Enabled = false;
			this.CmntColor.Location = new System.Drawing.Point(296, 56);
			this.CmntColor.Name = "CmntColor";
			this.CmntColor.Size = new System.Drawing.Size(152, 20);
			this.CmntColor.TabIndex = 5;
			this.CmntColor.Text = "";
			this.CmntColor.TextChanged += new System.EventHandler(this.CmntColor_TextChanged);
			// 
			// label6
			// 
			this.label6.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label6.Location = new System.Drawing.Point(208, 32);
			this.label6.Name = "label6";
			this.label6.Size = new System.Drawing.Size(100, 16);
			this.label6.TabIndex = 4;
			this.label6.Text = "Comments:";
			// 
			// CmntBtn
			// 
			this.CmntBtn.Location = new System.Drawing.Point(208, 56);
			this.CmntBtn.Name = "CmntBtn";
			this.CmntBtn.TabIndex = 3;
			this.CmntBtn.Text = "Color...";
			this.CmntBtn.Click += new System.EventHandler(this.CmntBtn_Click);
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.syntaxBox,
																					this.commentsBox});
			this.groupBox1.Location = new System.Drawing.Point(8, 16);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(184, 368);
			this.groupBox1.TabIndex = 2;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "Options";
			// 
			// syntaxBox
			// 
			this.syntaxBox.Location = new System.Drawing.Point(24, 56);
			this.syntaxBox.Name = "syntaxBox";
			this.syntaxBox.Size = new System.Drawing.Size(152, 24);
			this.syntaxBox.TabIndex = 1;
			this.syntaxBox.Text = "Syntax Highlighting On?";
			this.syntaxBox.CheckedChanged += new System.EventHandler(this.syntaxBox_CheckedChanged);
			// 
			// commentsBox
			// 
			this.commentsBox.Location = new System.Drawing.Point(24, 24);
			this.commentsBox.Name = "commentsBox";
			this.commentsBox.Size = new System.Drawing.Size(136, 24);
			this.commentsBox.TabIndex = 0;
			this.commentsBox.Text = "Commenting On?";
			this.commentsBox.CheckedChanged += new System.EventHandler(this.commentsBox_CheckedChanged);
			// 
			// EditorTab
			// 
			this.EditorTab.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.ApplyBtn,
																					this.ResetBtn,
																					this.label5,
																					this.FontLbl,
																					this.FontBtn,
																					this.label4,
																					this.BgLbl,
																					this.BgBtn});
			this.EditorTab.Location = new System.Drawing.Point(4, 22);
			this.EditorTab.Name = "EditorTab";
			this.EditorTab.Size = new System.Drawing.Size(464, 390);
			this.EditorTab.TabIndex = 2;
			this.EditorTab.Text = "Editor";
			// 
			// ApplyBtn
			// 
			this.ApplyBtn.Enabled = false;
			this.ApplyBtn.Location = new System.Drawing.Point(104, 136);
			this.ApplyBtn.Name = "ApplyBtn";
			this.ApplyBtn.TabIndex = 20;
			this.ApplyBtn.Text = "Apply";
			this.ApplyBtn.Click += new System.EventHandler(this.ApplyBtn_Click);
			// 
			// ResetBtn
			// 
			this.ResetBtn.Location = new System.Drawing.Point(8, 136);
			this.ResetBtn.Name = "ResetBtn";
			this.ResetBtn.Size = new System.Drawing.Size(80, 23);
			this.ResetBtn.TabIndex = 19;
			this.ResetBtn.Text = "Reset Colors";
			this.ResetBtn.Click += new System.EventHandler(this.ResetBtn_Click);
			// 
			// label5
			// 
			this.label5.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label5.Location = new System.Drawing.Point(8, 72);
			this.label5.Name = "label5";
			this.label5.Size = new System.Drawing.Size(128, 16);
			this.label5.TabIndex = 18;
			this.label5.Text = "Font Color:";
			// 
			// FontLbl
			// 
			this.FontLbl.Enabled = false;
			this.FontLbl.Location = new System.Drawing.Point(96, 96);
			this.FontLbl.Name = "FontLbl";
			this.FontLbl.Size = new System.Drawing.Size(136, 20);
			this.FontLbl.TabIndex = 17;
			this.FontLbl.Text = "";
			this.FontLbl.TextChanged += new System.EventHandler(this.FontLbl_TextChanged);
			// 
			// FontBtn
			// 
			this.FontBtn.Location = new System.Drawing.Point(8, 96);
			this.FontBtn.Name = "FontBtn";
			this.FontBtn.TabIndex = 16;
			this.FontBtn.Text = "Color ...";
			this.FontBtn.Click += new System.EventHandler(this.FontBtn_Click);
			// 
			// label4
			// 
			this.label4.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label4.Location = new System.Drawing.Point(8, 16);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(128, 16);
			this.label4.TabIndex = 15;
			this.label4.Text = "Background Color:";
			// 
			// BgLbl
			// 
			this.BgLbl.Enabled = false;
			this.BgLbl.Location = new System.Drawing.Point(96, 40);
			this.BgLbl.Name = "BgLbl";
			this.BgLbl.Size = new System.Drawing.Size(136, 20);
			this.BgLbl.TabIndex = 14;
			this.BgLbl.Text = "";
			this.BgLbl.TextChanged += new System.EventHandler(this.BgLbl_TextChanged);
			// 
			// BgBtn
			// 
			this.BgBtn.Location = new System.Drawing.Point(8, 40);
			this.BgBtn.Name = "BgBtn";
			this.BgBtn.TabIndex = 13;
			this.BgBtn.Text = "Color ...";
			this.BgBtn.Click += new System.EventHandler(this.BgBtn_Click);
			// 
			// BgColor
			// 
			this.BgColor.AllowFullOpen = false;
			this.BgColor.SolidColorOnly = true;
			// 
			// FontColor
			// 
			this.FontColor.AllowFullOpen = false;
			this.FontColor.SolidColorOnly = true;
			// 
			// Options
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(472, 413);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.tabControl1});
			this.Name = "Options";
			this.Text = "Options";
			this.tabControl1.ResumeLayout(false);
			this.KeywordTab.ResumeLayout(false);
			this.GeneralTab.ResumeLayout(false);
			this.groupBox1.ResumeLayout(false);
			this.EditorTab.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		// Allows user to select a customized XML Keyword file.
		private void openFileBtn_Click(object sender, System.EventArgs e)
		{
			openKeywordFile.Title = "XML Keyword Selection Dialog";
			openKeywordFile.InitialDirectory = @"c:\";
			openKeywordFile.Filter = "XML (*.xml)|*.xml|All (*.*)|*.*";
			openKeywordFile.RestoreDirectory = true;
			if (openKeywordFile.ShowDialog() == DialogResult.OK) 
			{
				custKeywordPath.Text = openKeywordFile.FileName;
			}

			keywordsHash = EditorConfiguration.ReadXmlToHash(custKeywordPath.Text);
			FillListBox(keywordsHash);
			ConfigHash["keywords"] = custKeywordPath.Text.ToString();
		}

		// Allows the user to add a value to the keyword XML file.
		private void AddBtn_Click(object sender, System.EventArgs e)
		{

			// Check to make sure the user typed a keyword to add.
			if (AddKeyword.Text == "")
			{
				return;
			}
			
			// Check to make sure we don't add the same keyword more than once.
			if (keywordsHash.ContainsKey(AddKeyword.Text.ToString()))
			{
				MessageBox.Show("Keyword file already contains an entry for the keyword: " + AddKeyword.Text.ToString());
				return;
			}

			// Add AddKeyword text to the keywordsHash and ListBox.
			listBox1.Items.Add(AddKeyword.Text.ToString());
			
			// Set the default color for this keyword to Blue.
			keywordsHash[AddKeyword.Text.ToString()] = "Blue";

			AddKeyword.Text = "";
		}

		// Allows the user to change the color value for a particular keyword.
		private void ColorBtn_Click(object sender, System.EventArgs e)
		{
			if (colorDialog1.ShowDialog() == DialogResult.OK)
			{
				string color = colorDialog1.Color.ToKnownColor().ToString();
				if (color == "0")
				{
					MessageBox.Show("The color you selected is not currently supported.");
					color = "Black";
				}
				keywordsHash[listBox1.SelectedItem.ToString()] = color;
				KeyColor.Text = keywordsHash[listBox1.SelectedItem.ToString()].ToString();
			}
		}

		// Removes an item from the ListBox.
		private void RemoveBtn_Click(object sender, System.EventArgs e)
		{
			// Make sure the user selected an item to remove.
			if (listBox1.SelectedIndex < 0)
			{
				return;
			}

			string item = listBox1.SelectedItem.ToString();
			listBox1.SelectedIndex = 0;
			if (keywordsHash.ContainsKey(item))
			{
				KeyColor.Text = keywordsHash[item].ToString();
			}
			listBox1.Items.Remove(item);
			keywordsHash.Remove(item.ToString());

			// If there is nothing else to remove, disable the "Remove" button.
			if (listBox1.Items.Count < 1)
			{
				RemoveBtn.Enabled = false;
			}
				
		}

		// Sets the KeyColor text field to the color of the given keyword
		// selected in the ListBox.
		private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			if (listBox1.SelectedItem == null)
			{
				return;
			}
			// If the keywordsHash contains the item in the ListBox, set the text
			// field to its color.
			if (keywordsHash.Contains(listBox1.SelectedItem.ToString()))
			{
				KeyColor.Text = keywordsHash[listBox1.SelectedItem.ToString()].ToString();
			}
				// If the keywordsHash does not contain the selected item, set the text
				// field to empty "".
			else
			{
				KeyColor.Text = "";
			}
		}

		// If the user clicks the "Cancel" button, close the "Options" window
		// without saving changes.
		private void CancelBtn_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		// Save all of the changes made in the "Options" and close the window.
		private void OKBtn_Click(object sender, System.EventArgs e)
		{
			EditorConfiguration.keywords = keywordsHash;
			EditorConfiguration info = new EditorConfiguration(this.ConfigPath, this.ConfigHash);
			info.SaveInfo();
			SaveKeywordOptions();
			code.SyntaxHighlight();
			Close();
		}

		// Saves Configuration changes.
		private void SaveKeywordOptions()
		{
			XmlTextWriter keywordWriter = null;
			keywordWriter = new XmlTextWriter(keywordXmlPath, null);

			try
			{
				keywordWriter.Formatting = Formatting.Indented;
				keywordWriter.Indentation= 6;
				keywordWriter.Namespaces = false;

				keywordWriter.WriteStartDocument();

				keywordWriter.WriteStartElement("", "keywords", "");

				// Loop through current keywordsHash.
				foreach (string key in keywordsHash.Keys)
				{
					// Write <word> element and value.
					keywordWriter.WriteStartElement("", "word", "");
					keywordWriter.WriteString(key.ToString());
					keywordWriter.WriteEndElement();

					// Write <color> element and value.
					keywordWriter.WriteStartElement("", "color", "");
					keywordWriter.WriteString(keywordsHash[key].ToString());
					keywordWriter.WriteEndElement();
				}

				keywordWriter.WriteEndElement();
				keywordWriter.Flush();
			}
			catch(Exception e)
			{
				MessageBox.Show("Error: " + e.Message + "\n" + e.Source + "\n" + e.TargetSite);
			}
			keywordWriter.Close();
		}

		// Updates the ConfigHash with the current Commenting value.
		private void commentsBox_CheckedChanged(object sender, System.EventArgs e)
		{
				ConfigHash["comments"] = commentsBox.Checked?"True":"False";
				EditorConfiguration.doComment = commentsBox.Checked;
		}

		// Updates the ConfigHash with the current Syntax Highlighting value.
		private void syntaxBox_CheckedChanged(object sender, System.EventArgs e)
		{
			ConfigHash["synHigh"] = syntaxBox.Checked?"True":"False";
			EditorConfiguration.doHighlight = syntaxBox.Checked;
		}

		// Sets the Background Color to the selected color.
		// If the color is unknown, it is set to White.
		private void BgBtn_Click(object sender, System.EventArgs e)
		{
			if (BgColor.ShowDialog() == DialogResult.OK)
			{
				string color = BgColor.Color.ToKnownColor().ToString();
				if (color == "0")
				{
					MessageBox.Show("The color you selected is not currently supported.");
					color = "White";
				}
				BgLbl.Text = color;
			}
		}

		// Sets the Font Color to the selected color.
		// If the color is unknown, it is set to Black.
		private void FontBtn_Click(object sender, System.EventArgs e)
		{
			if (FontColor.ShowDialog() == DialogResult.OK)
			{
				string color = FontColor.Color.ToKnownColor().ToString();
				if (color == "0")
				{
					MessageBox.Show("The color you selected is not currently supported.");
					color = "Black";
				}
				FontLbl.Text = color;
			}
		}

		// Returns the editor's background and font color to the default values.
		private void ResetBtn_Click(object sender, System.EventArgs e)
		{
			EditorConfiguration info = new EditorConfiguration(this.ConfigPath);
			this.BgLbl.Text = info.GetInfo("DefaultBg");
			this.FontLbl.Text = info.GetInfo("DefaultFont");		
		}

		// Update the ConfigHash with the new Background Color.
		private void BgLbl_TextChanged(object sender, System.EventArgs e)
		{
			this.ConfigHash["currBg"] = this.BgLbl.Text.ToString();
			this.ApplyBtn.Enabled = true;
		}

		// Update the ConfigHash with the new Font Color.
		private void FontLbl_TextChanged(object sender, System.EventArgs e)
		{
			this.ConfigHash["currFont"] = this.FontLbl.Text.ToString();
			this.ApplyBtn.Enabled = true;
		}

		// Apply the Background and Font colors.
		private void ApplyBtn_Click(object sender, System.EventArgs e)
		{
			code.BackColor = (Color)colConvert.ConvertFromString(BgLbl.Text.ToString());
			code.ForeColor = (Color)colConvert.ConvertFromString(FontLbl.Text.ToString());
			code.SyntaxHighlight();
		}

		// Select a color for the comments.
		private void CmntBtn_Click(object sender, System.EventArgs e)
		{
			if (CmntColorDlg.ShowDialog() == DialogResult.OK)
			{
				string color = CmntColorDlg.Color.ToKnownColor().ToString();
				if (color == "0")
				{
					MessageBox.Show("The color you selected is not currently supported.");
					color = "Black";
				}
				CmntColor.Text = color;
				this.code.ReCommentAll();

			}
		}

		// Comment Color Changes.
		private void CmntColor_TextChanged(object sender, System.EventArgs e)
		{
			this.ConfigHash["cmntColor"] = CmntColor.Text.ToString();
			this.code.ReCommentAll();
		}


	}
}
